#include "SDLImport.h"
#include "objectDetection.h"
#include "SDLDrawing.h"
#include <SDL_ttf.h>

//! \brief Program entry point
//!
//! \param argc The standard argument count (not used)
//! \param argv The standard arguments (not used)
//! \return Program result
int main ( int argc, char** argv )
{
    SDL_Init(SDL_INIT_VIDEO);                                                           //SDL Initialisation
    TTF_Init();                                                                         //Text Initialisation

    SDL_WM_SetCaption("Super TIPE !", NULL);


    Pixel** mat;                                                                        //Base Pixel Matrix
    SDL_Surface* bmp;
    if(argc < 2) {bmp = SDL_LoadBMP("logo.bmp");}
    else {bmp = SDL_LoadBMP(SDLFilenameCompliance(argv[1]));}
    if(!bmp) {printf("Impossible to load image"); return 1;}
    SDL_Surface* screen = SDL_SetVideoMode(bmp->w, bmp->h, 32, SDL_HWSURFACE);          //Screen initialization
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
    Taille size = {(unsigned short)bmp->h, (unsigned short)bmp->w};
    double tolerance = 12.0;
    mat = createMatFromSDL(bmp);                                                        //Get mat from image
    DrawParameters drawParams = initDrawing(mat, size);

    ObjectManager* objs = startDetection(mat, size, tolerance);                         //Creation of object manager

    bool done = false;

    drawScene(screen, objs, drawParams);

    SDL_Event event;
    while (!done)                                                                       //Main event loop
    {
        SDL_WaitEvent(&event);
        switch (event.type)     //We treat the event
        {
            case SDL_QUIT:
                done = true;
                break;

            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        done = true;
                        break;
                    case SDLK_1:
                        drawParams.drawMode.mainMode = OBJECT;                                     //Draw object
                        break;
                    case SDLK_2:
                        drawParams.drawMode.mainMode = ORIGINAL;                                   //Draw base image
                        break;
                    /*case SDLK_i:
                        drawParams.drawMode.drawIsobar = (!drawParams.drawMode.drawIsobar);        //Toggle isobar
                        break;*/
                    case SDLK_s:
                        drawParams.drawMode.drawSkel = (drawParams.drawMode.drawSkel + 1) % 3;            //Toggle skeleton
                        break;
                    case SDLK_F12:
                        saveResult(screen, (int)TOLERANCEL, (int)TOLERANCEab);          //Save results
                        break;

                    case SDLK_o:
                        tolerance += 1.0;
                        {
                        delete objs;
                        objs = startDetection(mat, size, tolerance);
                        }                 //Increase tolerance
                        break;
                    case SDLK_p:
                        if (tolerance > 1.0)
                        {tolerance -= 1.0;
                        delete objs;
                        objs = startDetection(mat, size, tolerance);
                        }                //Decrease tolerance
                        break;
                    case SDLK_3:
                        drawParams.drawMode.mainMode = HUE;
                        break;
                    case SDLK_4:
                        drawParams.drawMode.mainMode = SATURATION;
                        break;
                    case SDLK_5:
                        drawParams.drawMode.mainMode = VALUE;
                        break;
                    default: break;
                }
                drawScene(screen, objs, drawParams);
                break;
        }
    }

    SDL_FreeSurface(bmp);                                                               //Cleaning up the environment
    freeMatrix(mat, size);
    delete objs;

    TTF_Quit();
    SDL_Quit();
    printf("Exited cleanly\n");
    return 0;
}

